package pit.com.ordermobilesystem.adapter;

import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.adapter.CategoryAdapter.CategoryView;
import pit.com.ordermobilesystem.constant.OrderConstant;
import pit.com.ordermobilesystem.imageutil.ImageFetcher;
import pit.com.ordermobilesystem.imageutil.ImageResizer;
import pit.com.ordermobilesystem.provider.BaseProvider;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.provider.OrderMobileProvider.CategoryTable;
import pit.com.ordermobilesystem.provider.OrderMobileProvider.ItemTable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class OrderItemAdapter extends CursorAdapter{

	  private LayoutInflater mLayoutInflater;
	  private Context mContext;
	  private ImageResizer mImageWorker;
	  
	public OrderItemAdapter(Context context, Cursor c) {
		super(context, c,true);
		    mContext = context;
		    mLayoutInflater = LayoutInflater.from(context);
		    mImageWorker = new ImageFetcher(context, OrderConstant.WIDTH_THUMB, OrderConstant.HEIGHT_THUMB);
		    mImageWorker.setErrorImage(R.drawable.ic_loading);
		    mImageWorker.setLoadingImage(R.drawable.ic_loading);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ItemView item_view=(ItemView) view;
		ContentValues value=BaseProvider.getContentValues(context, cursor);
		final int idItem=value.getAsInteger(ItemTable.ID_ITEM);
		final ContentValues updateAddValue=new ContentValues();
		final ContentValues updateSubValue=new ContentValues();
		updateAddValue.put(ItemTable.NUMBER_ORDER, value.getAsInteger(ItemTable.NUMBER_ORDER)+1);
		if(value.getAsInteger(ItemTable.NUMBER_ORDER)>0){
			updateSubValue.put(ItemTable.NUMBER_ORDER, value.getAsInteger(ItemTable.NUMBER_ORDER)-1);
		}else{
			updateSubValue.put(ItemTable.NUMBER_ORDER, 0);
		}
		if(value.getAsInteger(ItemTable.NUMBER_ORDER)==0){
			item_view.lay_image.setVisibility(View.GONE);
		}else{
			item_view.lay_image.setVisibility(View.VISIBLE);
			item_view.tv_number.setText(value.getAsString(ItemTable.NUMBER_ORDER));
		}		
		item_view.tv_title.setText(cursor.getString(cursor.getColumnIndex(ItemTable.NAME_ITEM)));
		String img_item=cursor.getString(cursor.getColumnIndex(ItemTable.LINK_IMAGE));
		if(img_item!=null && !img_item.equals("")){

			mImageWorker.loadImage(img_item,item_view.img_item);
		}else{
			item_view.img_item.setImageResource(R.drawable.ic_apple);	
		}
		item_view.tv_description.setText(cursor.getString(cursor.getColumnIndex(ItemTable.DESCRIPTION)));
		item_view.tv_price.setText(cursor.getString(cursor.getColumnIndex(ItemTable.PRICE))+context.getString(R.string.d));
		item_view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mContext.getContentResolver().update(OrderMobileProvider.ITEM_CONTENT_URI, updateAddValue, ItemTable.ID_ITEM+"="+idItem,null);
				mContext.getContentResolver().notifyChange(OrderMobileProvider.ITEM_CONTENT_URI, null);
			}
		});
		item_view.tv_add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mContext.getContentResolver().update(OrderMobileProvider.ITEM_CONTENT_URI, updateAddValue, ItemTable.ID_ITEM+"="+idItem,null);
				mContext.getContentResolver().notifyChange(OrderMobileProvider.ITEM_CONTENT_URI, null);
			}
		});
		item_view.tv_sub.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mContext.getContentResolver().update(OrderMobileProvider.ITEM_CONTENT_URI, updateSubValue, ItemTable.ID_ITEM+"="+idItem,null);
				mContext.getContentResolver().notifyChange(OrderMobileProvider.ITEM_CONTENT_URI, null);
			}
		});		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return new ItemView(context);
	}
	
	class ItemView extends RelativeLayout {
		TextView tv_title;
		ImageView img_item;
		TextView tv_price;

		TextView tv_description;
		TextView tv_number;
		RelativeLayout lay_image;
		TextView tv_add;
		TextView tv_sub;
		public ItemView(Context context) {
			super(context);
			LayoutInflater li = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(R.layout.view_order_item, this, true);
			tv_title = (TextView) findViewById(R.id.tv_title);
			img_item = (ImageView) findViewById(R.id.img_item);
			tv_price = (TextView) findViewById(R.id.tv_price);
			
			tv_description = (TextView) findViewById(R.id.tv_description);
			tv_number=(TextView) findViewById(R.id.tv_number);
			lay_image=(RelativeLayout) findViewById(R.id.lay_image);
			tv_add=(TextView) findViewById(R.id.tv_plus);
			tv_sub=(TextView) findViewById(R.id.tv_sub);
		}
	}	

}
